package NWish.properties
{
	import NWish.Misc;
	import NWish.NWEvent;
	import NWish.properties.materials.*;
	
	import away3d.entities.Mesh;
	
	import org.aswing.JComboBox;
	import org.aswing.JToggleButton;

	public class MaterialPropertiesTypes
	{
		private var _window;
		
		private var _subMeshs:JComboBox
		//private var _subMeshIndex = -1;
		
		private var _types:Array;
		
		public function MaterialPropertiesTypes(parent,window)
		{
			//
			parent.addEventListener(NWEvent.SELSCT_OBJECT,onSelectObject);
			
			
			//
			_window = window;
			
			
			//
			var material:JToggleButton = new JToggleButton('    material     ');
			material.x = 80;
			material.y = 2;
			material.pack();
			material.height = 16;
			
			_window.buttonGroup.appendAll(material);
			_window.panel.append(material);
			material.addActionListener(onMaterial);
			
			//
			_subMeshs = new JComboBox();
			_subMeshs.setLocationXY(material.x + material.width, material.y);
			_subMeshs.setSizeWH(material.width,material.height);
			//_subMeshs.setEditable(true);
			_subMeshs.setMaximumRowCount(8);
			_subMeshs.getEditor().setValue('');
			_subMeshs.addActionListener(actionSubMaterials);
			_subMeshs.setFont(_subMeshs.getFont().changeSize(8));
			//_subMeshs.visible = false;
			_subMeshs.setToolTipText('_subMeshs');
			//_subMeshs.setListData([-1,0,1,2,3,4]);
			_subMeshs.setSelectedItem(-1);
			_window.panel.append(_subMeshs);
			
			
			
			//
			_types = 
				[new ColorMaterialProperties(parent,this)
				,new AnimatedBitmapMaterialProperties(parent,this)
				,new BitmapMaterialProperties(parent,this)
				,new SkyBoxMaterialProperties(parent,this)
				];
		}
		
		private function onMaterial(e)
		{
			if((e.target as JToggleButton).isSelected())
			{
				_window.currPropertiesTypes = this;
				
				if(_window.currObject == null)
					return;
				
				var submats = [-1];
				if(_window.currObject.hasOwnProperty('subMeshes') && _window.currObject.subMeshes.length > 1)
				{
					//_subMeshs.visible = true;
					for (var i = 0; i < _window.currObject.subMeshes.length; ++i)
						submats.push(i);
					_subMeshs.setListData(submats);
				}
				else
				{
					_subMeshs.setListData(submats);
				}
			}
		}
		
		private function onSelectObject(e:NWEvent)
		{
			_subMeshs.setSelectedItem(-1);
		}
		
		public function get subMeshIndex()
		{
			return _subMeshs.getSelectedItem();
		}
		private function actionSubMaterials(e)
		{
			//subMeshIndex = _subMeshs.getSelectedItem();
			_window.currObject = _window.currObject;
			//if(subMeshIndex != -1)
			//	_window.multiApplyButton.setEnabled(false);
		}
		
		public function get types()
		{
			return _types;
		}
		
		public function getPropertiesType(obj:Object)
		{
			var subMesh = subMeshIndex;
				
			if(obj.hasOwnProperty('material') == false)
				return null;
			
			var mat = obj.material;
			
			if(subMesh != null && subMesh != -1)
			{
				mat = obj.subMeshes[subMesh].material;
			}
			
			//
			try
			{
				var m = mat;
			}
			catch(e)
			{
				return null;
			}
			
			//
			for each (var p in _types)
			{
				
				if(Misc.isClass(mat,p.type))
				//if(mat is p.type)
				{
					return p;
				}
			}
			
			//
			return null;
		}
		
		public function toString()
		{
			return 'material properties';
		}
	}
}